/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view.layer;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

import soccerscope.model.ClientScene;
import soccerscope.model.Param;
import soccerscope.util.geom.Point2f;
import soccerscope.util.geom.Vector2f;
import soccerscope.view.FieldPane;

public class KickableLayer extends Layer {

	public KickableLayer(FieldPane fieldPane, boolean enable) {
		super(fieldPane, enable);
	}

	@Override
	public String getLayerName() {
		return "Kickable";
	}

	private boolean dtackle = true;

	private JCheckBox tackleCB;

	@Override
	public JPanel getConfigPanel() {
		JPanel panel = new JPanel();

		this.tackleCB = new JCheckBox("Tackle Area");
		this.tackleCB.addActionListener(new KickableLayerActionListener());
		panel.add(this.tackleCB);

		return panel;
	}

	class KickableLayerActionListener implements ActionListener {
		public void actionPerformed(ActionEvent ie) {
			Object o = ie.getSource();
			boolean b = ((AbstractButton) o).isSelected();
			if (o == KickableLayer.this.tackleCB)
				KickableLayer.this.dtackle = b;
		}
	}

	private Vector2f diff = new Vector2f();

	private Point2f nextPos = new Point2f();

	@Override
	public void draw(Graphics g) {
		if (!this.enable)
			return;

		ClientScene clientScene = this.fieldPane.getScene();
		float ballMagnify = this.fieldPane.getBallMagnify();
		float playerMagnify = this.fieldPane.getPlayerMagnify();
		for (int i = 0; i < Param.MAX_PLAYER * 2; i++) {
			if (!clientScene.player[i].isEnable())
				continue;

			float playerR = clientScene.player[i].getSize() * playerMagnify;
			Color drawColor = clientScene.player[i].getColor();

			// draw kickable and catchable area
			float drawR;
			drawR = Param.KICKABLE_R;
			if (clientScene.player[i].isKicking()) {
				if (clientScene.player[i].isKickFault())
					g.setColor(drawColor.darker().darker());
				else
					g.setColor(drawColor.darker());
				Graphics2D g2 = (Graphics2D) g;
				g2.setComposite(AlphaComposite.getInstance(
						AlphaComposite.SRC_OVER, 0.3f));
				fillCircle(g, clientScene.player[i].pos, drawR);
				g2.setComposite(AlphaComposite.getInstance(
						AlphaComposite.SRC_OVER, 1.0f));
			}
			g.setColor(drawColor);
			drawCircle(g, clientScene.player[i].pos, drawR);

			if (clientScene.player[i].isGoalie()) {
				drawR = Param.GOALIE_CATCHABLE_AREA_LENGTH;
				if (clientScene.player[i].isCatching()) {
					if (clientScene.player[i].isCatchFault())
						g.setColor(drawColor.darker().darker());
					else
						g.setColor(drawColor.darker());
					fillCircle(g, clientScene.player[i].pos, drawR);
				}
				g.setColor(drawColor);
				drawCircle(g, clientScene.player[i].pos, drawR);
			}

			if (clientScene.player[i].isTackling()) {
				if (clientScene.player[i].isTackleFault())
					g.setColor(drawColor.darker().darker());
				else
					g.setColor(drawColor.darker());
				if (this.dtackle) {
					fillRect(g, new Point2f(clientScene.player[i].pos.x
							- Param.TACKLE_BACK_DIST,
							clientScene.player[i].pos.y - Param.TACKLE_WIDTH
									/ 2.0f), new Point2f(Param.TACKLE_DIST
							+ Param.TACKLE_BACK_DIST, Param.TACKLE_WIDTH),
							(int) clientScene.player[i].angle,
							clientScene.player[i].pos);
				}
			}

			g.setColor(drawColor);
			if (this.dtackle) {
				drawRect(g, new Point2f(clientScene.player[i].pos.x
						- Param.TACKLE_BACK_DIST, clientScene.player[i].pos.y
						- Param.TACKLE_WIDTH / 2.0f), new Point2f(
						Param.TACKLE_DIST + Param.TACKLE_BACK_DIST,
						Param.TACKLE_WIDTH), (int) clientScene.player[i].angle,
						clientScene.player[i].pos);
			}

			if (clientScene.player[i].isKickable(clientScene.ball.pos)) {
				Vector2f acc = clientScene.player[i].getKickAccelerate(
						clientScene.ball.pos, 100.0f, 0.0f);
				float kickableRange = acc.length();
				this.nextPos.add(clientScene.ball.pos, clientScene.ball.vel);
				drawCircle(g, this.nextPos, kickableRange);
			}

			if (this.dtackle
					&& clientScene.player[i].canTackle(clientScene.ball.pos)) {
				float prob = clientScene.player[i]
						.getTackleProb(clientScene.ball.pos);
				Point2f drawPoint = new Point2f();
				Point2f drawOffset = new Point2f(0.0f, playerR * 2.0f);
				drawPoint.add(clientScene.player[i].pos, drawOffset);
				drawString(g, String.valueOf(prob), drawPoint);
			}
		}
	}
}
